#include <stdlib.h>
#include <GL/glut.h>
#include <GL/glext.h>
#include "TextureObject.hpp"
#include "FreeImage.h"

TextureObject::TextureObject( std::string fileName )
{
	//! Use FreeImage library to recognize image file type.
	FREE_IMAGE_FORMAT fileFormat = FreeImage_GetFileType( fileName.c_str() );

	//! Use FreeImage library to load data from image file.
	FIBITMAP* bitmap = FreeImage_Load( fileFormat, fileName.c_str() );

	//! Generate texture with loaded data.
	glGenTextures( 1, &m_textureID );
	glBindTexture( GL_TEXTURE_2D, m_textureID );

	glTexImage2D( GL_TEXTURE_2D, 0, 3, FreeImage_GetWidth( bitmap ), FreeImage_GetHeight( bitmap ), 0, GL_BGR_EXT,
		          GL_UNSIGNED_BYTE, FreeImage_GetBits( bitmap ) );

	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

	//! Delete unnecessary data.
	FreeImage_Unload( bitmap );
}

TextureObject::~TextureObject()
{
	//! Delete texture from video RAM.
	glDeleteTextures( 1, &m_textureID );
}

const GLuint& TextureObject::GetTextureID()
{
	//! Return ID of texture.
	return m_textureID;
}